package chat.util;


import chat.bean.Message;
import chat.constant.CommonConst;

import java.io.*;
import java.util.Optional;

public class MsgUtils {

    public static Optional<Message> readMsg(InputStream inputStream) {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(inputStream);
            return Optional.ofNullable((Message) ois.readObject());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void writeMsg(OutputStream outputStream, Message message) {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(outputStream);
            oos.writeObject(message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Message createMsg(Integer type, String content, String username) {
        return new Message(type, content, username);
    }
    public static Message createFriendMsg(Integer type, String content, String username,String friendName) {
        return new Message(type, content, username, CommonConst.BLACK,friendName);
    }
    public static Message createMsg(Integer type, String content, String username,String password) {
        return new Message(type, content, username,password);
    }
}
